package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func healthz(w http.ResponseWriter, r*http.Request) {
	for name, value := range r.Header {
		fmt.Printf("%v: %v\n", name, value)
	}

	// 遍历所有请求头
	for k, v := range r.Header {
		w.Header().Set(k,v[0])
		//fmt.Fprintf(w, "Header field %q, Value %q\n", k, v)
	}

	goPath := os.Getenv("GOPATH")
	//fmt.Printf("GOPATH is %s\n",goPath)
	w.Header().Set("GOPATH",goPath)

	fmt.Fprintf(w, "Method: %s URL: %s Protocol: %s \n", r.Method, r.URL, r.Proto)
	fmt.Fprintf(w, "Host = %q\n", r.Host)
	w.WriteHeader(200)
	fmt.Fprintf(w, "RemoteAddr= %q\n", r.RemoteAddr)
}

func main() {

	http.HandleFunc("/", handler)
	//注册 handle 处理函数
	http.HandleFunc("/healthz", healthz)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}
}